package com.example.controller;

import com.example.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/payment")
public class PaymentController {
    @Autowired
    private AlipayService alipayService;

    @PostMapping("/alipay")
    public String createAlipayPayment(@RequestBody Map<String, Object> request) {
        try {
            List<Map<String, Object>> items = (List<Map<String, Object>>) request.get("items");
            String totalAmount = (String) request.get("totalAmount");
            return alipayService.createPayment(items, totalAmount);
        } catch (Exception e) {
            throw new RuntimeException("创建支付订单失败", e);
        }
    }

    @PostMapping("/alipay/notify")
    public String handleAlipayNotify(@RequestParam Map<String, String> params) {
        try {
            alipayService.handlePaymentNotify(params);
            return "success";
        } catch (Exception e) {
            return "fail";
        }
    }
} 